package com.example.application22;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageView main_iv_setting;
    private TextView main_tv_temp;
    private TextView main_tv_hum;
    private TextView mian_tv_body;
    private TextView main_tv_light;
    private LinearLayout main_ll_bj;
    String ip;
    String port;
    String token;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        main_iv_setting = findViewById(R.id.main_iv_setting);
        main_tv_temp = findViewById(R.id.main_tv_temp);
        main_tv_hum = findViewById(R.id.main_tv_hum);
        mian_tv_body = findViewById(R.id.mian_tv_body);
        main_tv_light = findViewById(R.id.main_tv_light);
        main_ll_bj = findViewById(R.id.main_ll_bj);

        main_iv_setting.setOnClickListener(this);

        //获取token
        new Thread(){
            @Override
            public void run() {
                super.run();
                SharedPreferences config = getSharedPreferences("config", MODE_PRIVATE);
                ip = config.getString(Config.setting_ed_ip, "");
                port = config.getString(Config.setting_ed_port, "");
                String username = config.getString(Config.setting_ed_username, "");
                String pwd = config.getString(Config.setting_ed_pwd, "");
                String login_url = "http://"+ ip +":"+ port +"/Users/Login";
                JSONObject jsonObject = new JSONObject();
                try {
                    jsonObject.put("Account",username);
                    jsonObject.put("Password",pwd);
                    jsonObject.put("IsRememberMe",true);
                    String pamer = jsonObject.toString();
                    String login_json = HttpUrl.post(login_url, pamer, null);
                    Log.i("login_json",login_json);
                    JSONObject jsonObject1 = new JSONObject(login_json);
                    JSONObject resultObj = jsonObject1.getJSONObject("ResultObj");
                    String accessToken = resultObj.getString("AccessToken");
                    SharedPreferences.Editor edit = config.edit();
                    edit.putString(Config.token,accessToken);
                    edit.commit();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();

        //获取温度
        new Thread(){
            @Override
            public void run() {
                super.run();
                while(true){
                    SharedPreferences config = getSharedPreferences("config", MODE_PRIVATE);
                    String wangguan_id = config.getString(Config.setting_ed_wangguan, "");
                    String temp = config.getString(Config.setting_ed_temp, "");
                    String fans = config.getString(Config.setting_ed_fan, "");
                    token = config.getString(Config.token, "");
                    String temp_url = "http://"+ ip +":"+ port +"/devices/"+ wangguan_id +"/Sensors/"+ temp +"?deviceId="+ wangguan_id +"&apiTag="+ temp;
                    //String temp_url = "http://"+ ip +"/devices/"+ wangguan_id +"/Datas";
                    String temp_json = HttpUrl.get(temp_url, token);
                    try {
                        JSONObject jsonObject = new JSONObject(temp_json);
                        JSONObject resultObj = jsonObject.getJSONObject("ResultObj");
                        String temp_value = resultObj.getString("Value");
                        Double aDouble = Double.valueOf(temp_value);
                        if(aDouble > 27){
                            String fans_url = "http://"+ ip +"/Cmds?deviceId="+ wangguan_id +"&apiTag="+ fans;
                            HttpUrl.post(fans_url,"1",token);
                        }else{
                            String fans_url = "http://"+ ip +"/Cmds?deviceId="+ wangguan_id +"&apiTag="+ fans;
                            HttpUrl.post(fans_url,"0",token);
                        }
                        Message message = new Message();
                        message.what = 1;

                        Bundle bundle = new Bundle();
                        bundle.putString("temp_value",temp_value);
                        message.setData(bundle);

                        handler.sendMessage(message);

                        Thread.sleep(5000);
                    } catch (Exception e) {
                        Log.e("err","错误");
                    }
                }
            }
        }.start();

        //获取湿度
        new Thread(){
            @Override
            public void run() {
                super.run();
                while(true){
                    SharedPreferences config = getSharedPreferences("config", MODE_PRIVATE);
                    String wangguan_id = config.getString(Config.setting_ed_wangguan, "");
                    String hum = config.getString(Config.setting_ed_hum, "");
                    token = config.getString(Config.token, "");
                    String hum_url = "http://"+ ip +":"+ port +"/devices/"+ wangguan_id +"/Sensors/"+ hum +"?deviceId="+ wangguan_id +"&apiTag="+ hum;
                    //String hum_url = "http://"+ ip +"/devices/"+ wangguan_id +"/Datas";
                    String hum_json = HttpUrl.get(hum_url, token);
                    Log.i("湿度json",hum_json);
                    try {
                        JSONObject jsonObject = new JSONObject(hum_json);
                        JSONObject resultObj = jsonObject.getJSONObject("ResultObj");
                        String hum_value = resultObj.getString("Value");

                        Message message = new Message();
                        message.what = 2;

                        Bundle bundle = new Bundle();
                        bundle.putString("hum_value",hum_value);
                        message.setData(bundle);

                        handler.sendMessage(message);

                        Thread.sleep(5000);
                    } catch (Exception e) {
                        Log.e("err","错误");
                    }
                }
            }
        }.start();

        //获取人体
        new Thread(){
            @Override
            public void run() {
                super.run();
                int index = 0;
                while(true){
                    SharedPreferences config = getSharedPreferences("config", MODE_PRIVATE);
                    String wangguan_id = config.getString(Config.setting_ed_wangguan, "");
                    String body = config.getString(Config.setting_ed_body, "");
                    String greend = config.getString(Config.setting_ed_greend, "");
                    token = config.getString(Config.token, "");
                    //String body_url = "http://"+ ip +":"+ port +"/devices/"+ wangguan_id +"/Sensors/"+ hum +"?deviceId="+ wangguan_id +"&apiTag="+ hum;
                    //String body_url = "http://"+ ip +"/devices/"+ wangguan_id +"/Datas";
                    String body_url = "http://"+ ip +"/devices/"+ wangguan_id +"/Sensors?deviceId="+ wangguan_id +"&apiTags="+ body;
                    String body_json = HttpUrl.get(body_url, token);
                    Log.i("人体json",body_json);
                    try {
                        JSONObject jsonObject = new JSONObject(body_json);
                        JSONArray resultObj = jsonObject.getJSONArray("ResultObj");
                        JSONObject jsonObject1 = resultObj.getJSONObject(0);
                        String body_value = jsonObject1.getString("Value");
                        Integer integer = Integer.valueOf(body_value);
                        if(integer == 1){
                            index = 0;
                            String greend_url = "http://"+ ip +"/Cmds?deviceId="+ wangguan_id +"&apiTag="+ greend;
                            HttpUrl.post(greend_url,"1",token);
                        }else{
                            index ++;
                            String greend_url = "http://"+ ip +"/Cmds?deviceId="+ wangguan_id +"&apiTag="+ greend;
                            HttpUrl.post(greend_url,"0",token);
                        }

                        if(index >= 12){
                            Message message = new Message();
                            message.what = 5;

                            handler.sendMessage(message);
                        }
                        Message message = new Message();
                        message.what = 3;

                        Bundle bundle = new Bundle();
                        bundle.putString("body_value",body_value);
                        message.setData(bundle);

                        handler.sendMessage(message);
                        Thread.sleep(5000);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();

        //获取光照
        new Thread(){
            @Override
            public void run() {
                super.run();
                while(true){
                    SharedPreferences config = getSharedPreferences("config", MODE_PRIVATE);
                    String wangguan_id = config.getString(Config.setting_ed_wangguan, "");
                    String light = config.getString(Config.setting_ed_light, "");
                    String greend = config.getString(Config.setting_ed_greend, "");
                    token = config.getString(Config.token, "");
                    //String body_url = "http://"+ ip +":"+ port +"/devices/"+ wangguan_id +"/Sensors/"+ hum +"?deviceId="+ wangguan_id +"&apiTag="+ hum;
                    //String body_url = "http://"+ ip +"/devices/"+ wangguan_id +"/Datas";
                    String light_url = "http://"+ ip +"/devices/"+ wangguan_id +"/Sensors?deviceId="+ wangguan_id +"&apiTags="+ light;
                    String light_json = HttpUrl.get(light_url, token);
                    Log.i("光照json",light_json);
                    try {
                        JSONObject jsonObject = new JSONObject(light_json);
                        JSONArray resultObj = jsonObject.getJSONArray("ResultObj");
                        JSONObject jsonObject1 = resultObj.getJSONObject(0);
                        String light_value = jsonObject1.getString("Value");
                        Double gz_value = Double.valueOf(light_value);
                        if(gz_value < 100){
                            String greend_url = "http://"+ ip +"/Cmds?deviceId="+ wangguan_id +"&apiTag="+ greend;
                            HttpUrl.post(greend_url,"1",token);
                        }else{
                            String greend_url = "http://"+ ip +"/Cmds?deviceId="+ wangguan_id +"&apiTag="+ greend;
                            HttpUrl.post(greend_url,"0",token);
                        }

                        Message message = new Message();
                        message.what = 4;

                        Bundle bundle = new Bundle();
                        bundle.putString("light_value",light_value);
                        message.setData(bundle);

                        handler.sendMessage(message);
                        Thread.sleep(5000);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();
    }

    @Override
    public void onClick(View v) {
        switch(v.getId()){
            case R.id.main_iv_setting:
                Intent intent = new Intent(MainActivity.this,SettingActivity.class);
                startActivity(intent);
                break;
        }
    }

    Handler handler = new Handler(Looper.myLooper()){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 1:
                    Bundle temp = msg.getData();
                    String temp_value = temp.getString("temp_value");
                    main_tv_temp.setText(temp_value);
                    break;
                case 2:
                    Bundle hum = msg.getData();
                    String hum_value = hum.getString("hum_value");
                    main_tv_hum.setText(hum_value);
                    break;
                case 3:
                    Bundle body = msg.getData();
                    String body_value = body.getString("body_value");
                    Integer integer = Integer.valueOf(body_value);
                    if(integer == 1){
                        mian_tv_body.setText("有人");
                        main_ll_bj.setBackgroundResource(R.mipmap.tvcopen);
                    }else if(integer == 0){
                        mian_tv_body.setText("无人");
                    }
                    break;
                case 4:
                    Bundle light = msg.getData();
                    String light_value = light.getString("light_value");
                    main_tv_light.setText(light_value);
                    break;
                case 5:
                    main_ll_bj.setBackgroundResource(R.mipmap.tvclose);
                    break;
            }
        }
    };

}